package com.chenailin.www.controller;

import com.chenailin.www.dao.AppointmentDAO;
import com.chenailin.www.daoimpl.AppointmentDAOImpl;
import com.chenailin.www.pojo.Appointment;
import com.chenailin.www.pojo.Rating;
import com.chenailin.www.pojo.Student;
import com.chenailin.www.service.*;

import java.util.Scanner;

import static com.chenailin.www.util.MenuUtil.getValidChoice;

public class RatingController {
    private final RatingService ratingService = new RatingService();
    private final Scanner sc = new Scanner(System.in);
    private final DoctorService doctorService = new DoctorService();
    private final DepartmentService departmentService = new DepartmentService();
    private final AppointmentService appointmentService = new AppointmentService();
    private final AppointmentController appointmentController = new AppointmentController();
    private final AppointmentDAO appointmentDAO = new AppointmentDAOImpl();

    // 添加评价
    public void addRating(Student student) {
        System.out.println("\n=== 评价系统 ===");
        System.out.println("\n=== 可以评价的服务 ===");

        appointmentController.listAppointments(student);

        System.out.print("请输入想评价的预约号ID: ");
        int id = sc.nextInt();
        sc.nextLine();
        if (!appointmentService.ifIdExists(id, appointmentService.getUserAppointments(student.getStuId()))) {
            System.out.println("该预约号不在评价范围内！");
            return;
        }
        System.out.print("请为服务质量打分: ");
        int serviceQuality = getValidChoice(1,5);
        System.out.print("请为环境打分: ");
        int environment = getValidChoice(1,5);
        System.out.print("请输入评价意见(可选): ");
        String comment = sc.nextLine();
        Rating rating = new Rating();
        Appointment appointment = appointmentDAO.findById(id);
        rating.setDepartmentId(appointment.getDepartmentId());
        rating.setDoctorId(appointment.getDoctorId());
        rating.setServiceQuality(serviceQuality);
        rating.setEnvironment(environment);
        rating.setComment(comment);
        rating.setStuId(student.getStuId());
        try {
            if (ratingService.addRating(rating)) {
                System.out.println("评价提交成功！");
            } else {
                System.out.println("评价提交失败");
            }
        } catch (IllegalArgumentException e) {
            System.out.println("输入错误: " + e.getMessage());
        }
    }

    // 查看医生评分
    public void showDoctorDetail() {
        System.out.print("输入医生ID: ");
        int id = sc.nextInt();
        sc.nextLine();
        System.out.println("\n=== 评价详情 ===");
        if (ratingService.getDoctorRating(id) == null) {
            System.out.println("(暂无评价信息)\n");
            return;
        }
        System.out.printf("%d. %s\n", id, doctorService.getDoctorDetail(id).getDocName());
        System.out.printf("平均评分:%.1f\n", ratingService.calculateDoctorRating(id));
        System.out.println("详细评论：");
        ratingService.getDoctorRating(id).forEach(r -> {
            if (r.getComment() != null && !r.getComment().isEmpty()) {
                System.out.printf("-%s\n", r.getComment());
            } else {
                System.out.println("(暂无评价信息)\n");
            }
        });
    }

    // 查看科室评分
    public void showDepartmentDetail() {
        System.out.print("输入科室ID: ");
        int id = sc.nextInt();
        sc.nextLine();
        System.out.println("\n=== 评价详情 ===");
        if (ratingService.getRatingsByDepartment(id) == null) {
            System.out.println("(暂无评价信息)\n");
            return;
        }
        System.out.printf("%d. %s\n", id, departmentService.getDepartmentDetail(id).getDepartName());
        System.out.printf("平均评分:%.1f\n", ratingService.calculateDepartmentRating(id));
        System.out.println("详细评论：");
        ratingService.getRatingsByDepartment(id).forEach(r -> {
            if (r.getComment() != null && !r.getComment().isEmpty()) {
                System.out.printf("-%s\n", r.getComment());
            } else {
                System.out.println("(暂无评价信息)\n");
            }
        });
    }
}
